package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.Brochure;
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.events.PreRenderBrochureEvent;
	import com.ettractions.kiosk.events.SelectBrochureEvent;
	import com.ettractions.kiosk.events.ViewStateEvent;
	import com.ettractions.kiosk.models.KioskModel;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.events.MouseEvent;
	
	import mx.core.ClassFactory;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	import spark.events.IndexChangeEvent;
	import spark.utils.BitmapUtil;
	
	public class BrochureMediator extends Mediator
	{
		[Inject]
		public var model:KioskModel;
		
		[Inject]
		public var view:Brochure;
		public var event:BrochureSelectedEvent;
		
		private var _subviews:Vector.<Group>;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(PreRenderBrochureEvent.PRE_RENDER_BROCHURE, handlePreRenderBrochure);
			eventDispatcher.addEventListener(ViewStateEvent.PRE_RENDERING_COMPLETE, handlePreRenderingComplete);
			eventDispatcher.addEventListener(BrochureSelectedEvent.BROCHURE_SELECTED, handleBrochureSelected);
			view.specials.sendOfferButton.addEventListener(MouseEvent.CLICK, handleSendOfferButtonClick);
			
			view.previousButton.addEventListener(MouseEvent.CLICK, handlePreviousNextButtonClick);
			view.nextButton.addEventListener(MouseEvent.CLICK, handlePreviousNextButtonClick);

			view.menu.selectedIndex = -1;
			view.menu.addEventListener(IndexChangeEvent.CHANGE, handleMenuIndexChange);
			
			_subviews = new <Group>[
				view.about,
				view.mapDirections,
				view.sendToMe,
				view.bookingInfo,
				view.specials
			];
			
			viewIndex = 0;

		}
				
		private function set viewIndex(index:int):void
		{
			view.menu.selectedIndex = index;
			
			for (var i:int=0; i<_subviews.length; i++)
				_subviews[i].visible = (i == index);
			
			view.media.visible = (index != 2);			
		}
		
		private var _lastPreRenderBrochure:BrochureVO;
		
		private function handlePreRenderBrochure(event:PreRenderBrochureEvent):void
		{
			// Assume the last brochure that was pre-rendered is ready now and store a bitmap snapshot of the view
			if (_lastPreRenderBrochure)
				_lastPreRenderBrochure.media.preRenderedCoverFlowItem = BitmapUtil.getSnapshot(view.content);

			updateView(event.brochure);
			// Re-populate the view
			view.data = _lastPreRenderBrochure = event.brochure;
		}
		
		private function handlePreRenderingComplete(event:ViewStateEvent):void
		{
			// Store a snapshot of the final brochure
			_lastPreRenderBrochure.media.preRenderedCoverFlowItem = BitmapUtil.getSnapshot(view.content);
		}
		
		private function handleSendOfferButtonClick(event:MouseEvent):void
		{
			viewIndex = 2;
		}
		
		private function updateView(brochure:BrochureVO):void
		{
			var special:Boolean = brochure.specialTag == "TRUE" ? true : false;
	
//			view.media.mediatest.text = brochure.specialTag;
		}

		private function handleBrochureSelected(event:BrochureSelectedEvent):void
		{
			if (event.brochure != null)
			{
				updateView(event.brochure);
				viewIndex = 0;
				
				view.data = event.brochure;
			}
		}

		private function handlePreviousNextButtonClick(event:MouseEvent):void
		{
			var offset:int;
			
			switch (event.target)
			{
				case view.previousButton:
					offset = -1;
					break;
				
				case view.nextButton:
					offset = 1;
					break;				
			}
			
			eventDispatcher.dispatchEvent(new SelectBrochureEvent(SelectBrochureEvent.SELECT_BROCHURE, null, offset));
		}
		
		private function handleMenuIndexChange(event:IndexChangeEvent):void
		{
			viewIndex = event.newIndex;
		}

	}
}